package com.geovis.emergency.spd.entity.command.pojo.vo;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillDutyOrgUserVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillDutyOrgVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 指挥系统组织架构vo对象
 */
@Getter
@Setter
@ToString(callSuper = true)
@Accessors(chain = true)
@ApiModel(value = "CommandDrillOrgVo", description = "指挥系统-演练组织架构VO")
public class CommandDrillOrgVo {

    @ApiModelProperty("主键")
    private String id;

    @ApiModelProperty("父主键")
    private String parentId;

    @ApiModelProperty("机构名称")
    private String orgName;

    @ApiModelProperty("排序下标")
    private Long idx;

    @ApiModelProperty("remark")
    private String remark;

    @ApiModelProperty(value = "职责")
    private String duty;

    @ApiModelProperty(value = "子节点列表")
    private List<CommandDrillOrgVo> children= Collections.emptyList();

    @ApiModelProperty("组织机构人员")
    private List<CommandDrillOrgUserVo> orgUsers= Collections.emptyList();


    public static CommandDrillOrgVo fromPcDrillDutyOrgVO(PcDrillDutyOrgVO pddov)
    {
        CommandDrillOrgVo cdov=new CommandDrillOrgVo();
        BeanUtils.copyProperties(pddov,cdov);
        if(!CollectionUtils.isEmpty(pddov.getDutyOrgDeptList()))
        {
            List<CommandDrillOrgUserVo> users=new ArrayList<>();
            pddov.getDutyOrgDeptList().stream().forEach(orgUsers->{
                String[] ids=orgUsers.getPrincipalIds().split(",");
                String[] names=orgUsers.getPrincipalNames().split(",");
                String[] phones=null;
                if(StringUtils.hasText(orgUsers.getPrincipalPhones()))
                {
                    phones=orgUsers.getPrincipalPhones().split(",");
                }
                for(int x=0;x<ids.length;x++)
                {
                    CommandDrillOrgUserVo user=new CommandDrillOrgUserVo();
                    user.setId(ids[x])
                            .setRealName(names[x])
                            .setJob(pddov.getOrgName());
                    if(phones!=null && !"null".equals(phones[x]))
                    {
                        user.setPhone(phones[x]);
                    }
                    users.add(user);
                }

            });
            cdov.setOrgUsers(users);
        }
        if(CollectionUtils.isEmpty(pddov.getChildren()))
        {
            return cdov;
        }
        List<CommandDrillOrgVo> cdovChild=pddov.getChildren().stream().map(children->CommandDrillOrgVo.fromPcDrillDutyOrgVO(children)).collect(Collectors.toList());
        cdov.setChildren(cdovChild);
        return cdov;
    }

}
